package com.douing.ad.dsp.bidding.rtb;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.douing.ad.dsp.bidding.dto.AdRequestDto;
import com.douing.ad.dsp.bidding.dto.AdResponseDto;
import com.douing.ad.dsp.bidding.dto.Admt;
import com.douing.ad.dsp.bidding.dto.AdsDto;
import com.douing.ad.dsp.bidding.dto.AppRequestDto;
import com.douing.ad.dsp.bidding.dto.AppResponseDto;
import com.douing.ad.dsp.bidding.dto.Bid;
import com.douing.ad.dsp.bidding.dto.MatchedCreativeDto;
import com.douing.ad.dsp.bidding.dto.SeatBid;
import com.douing.ad.dsp.bidding.tj.utils.NodeData;
import com.douing.ad.dsp.bidding.dto.AssetResponse;
import com.douing.ad.dsp.bidding.dto.AssetResponseDesc;
import com.douing.ad.dsp.bidding.dto.AssetResponseImg;
import com.douing.ad.dsp.bidding.dto.AssetResponseTitle;
import com.douing.ad.dsp.bidding.dto.BidNative;
import com.douing.ad.dsp.bidding.dto.LinkDto;

public class GenerateResponse {
	public static AdResponseDto BuildResponse(AdRequestDto request, MatchedCreativeDto finalCreative, NodeData nodeData,
			Map<String, NodeData> nodeDataMap) {
		AdResponseDto response = new AdResponseDto();
		response.setBidding(false);
		response.setRequestId(request.getRequestId());
		// response.setCategory(request.getCategory());
		response.setCookieId(request.getUser().getUserId());
		response.setVersion(request.getVersion());

		response.setReqTime(request.getReqTime());
		AdsDto ad = new AdsDto();
		if (finalCreative == null) {
			return response;
		}
		response.setMediaId(finalCreative.getMediaId());
		response.setAreaId(finalCreative.getAreaId());
		response.setCategory(new Long[] { finalCreative.getMatchedcatid() });
		ad.setAdId(request.getImp().getAdSlotId());
		ad.setImpId(request.getImp().getImpId());
		ad.setCreativeId(String.valueOf(finalCreative.getId()));
		ad.setAdDomain("");
		ad.setPrice(finalCreative.getBidPrice());
		ad.setMaxPrice(finalCreative.getMaxPrice());
		ad.setAdmt(finalCreative.getAdmt());
		ad.setAdUrl(finalCreative.getUrl());
		ad.setDestinationUrl(finalCreative.getTargetUrl());
		ad.setAdWidth(request.getImp().getBanner().getWidth());
		ad.setAdHeight(request.getImp().getBanner().getHeight());
		ad.setAdvertiserId(finalCreative.getCreateUserId());
		ad.setCreativeTradeId(finalCreative.getCreativeTradeId());
		response.setAds(new AdsDto[] { ad });
		response.setBidding(true);
		Common.WriteLog("bidding finish and success! requestid is " + request.getRequestId() + ";advertiserid is "
				+ ad.getAdvertiserId() + ";bidprice is " + finalCreative.getBidPrice());
		// TODO
		NodeData nd = nodeDataMap.get(finalCreative.getId() + "");
		nd.setExitNum(1);

		return response;
	}

	public static AppResponseDto BuildAppResponse(AppRequestDto request, List<MatchedCreativeDto> finalCreatives,
			NodeData nodeData, Map<String, NodeData> nodeDataMap) {
		AppResponseDto response = new AppResponseDto();
		response.setBidding(false);
		response.setRequestId(request.getRequestId());
		// response.setCookieId(request.getUser().getUserId());

		response.setReqTime(request.getReqTime());
		if (finalCreatives == null || finalCreatives.size() == 0) {
			return response;
		}
		boolean hasarea = false;
		List<Bid> ads = new ArrayList<Bid>();
		for (MatchedCreativeDto finalCreative : finalCreatives) {
			if (!hasarea) {
				response.setAreaId(finalCreative.getAreaId());
				response.setAppCat(String.valueOf(finalCreative.getMatchedcatid()));
				hasarea = true;
			}

			response.setAppId(finalCreative.getMediaId());

			Bid ad = new Bid();
			ad.setImpId(finalCreative.getImpid());
			ad.setCid(String.valueOf(finalCreative.getId()));
			ad.setCrid(String.valueOf(finalCreative.getId()));
			ad.setAdDomain("");
			ad.setPrice(finalCreative.getBidPrice());
			ad.setMaxPrice(finalCreative.getMaxPrice());
			// 原生广告处理
			if (finalCreative.getAdmt() == Admt.NATIVE) {
				// ad.setAdmt(8);
				BidNative bidNative = new BidNative();
				List<AssetResponse> assets = new ArrayList<AssetResponse>();
				// title
				AssetResponse assetTitle = new AssetResponse();
				AssetResponseTitle title = new AssetResponseTitle();
				title.setText(finalCreative.getTitle());
				assetTitle.setTitle(title);
				assets.add(assetTitle);
				// desc
				AssetResponse assetDesc = new AssetResponse();
				AssetResponseDesc desc = new AssetResponseDesc();
				desc.setText(finalCreative.getDesc());
				assetDesc.setDesc(desc);
				assets.add(assetDesc);
				// img
				if (finalCreative.getNativeUrls() != null) {
					for (String url : finalCreative.getNativeUrls()) {
						AssetResponse assetImg = new AssetResponse();
						AssetResponseImg img = new AssetResponseImg();
						img.setHeight(Float.valueOf(finalCreative.getHeight()).intValue());
						img.setWidth(Float.valueOf(finalCreative.getWidth()).intValue());
						img.setUrl(url);
						assetImg.setImg(img);
						assets.add(assetImg);
					}
				}
				// icon
				if (finalCreative.getIconH() != 0) {
					AssetResponse assetIcon = new AssetResponse();
					AssetResponseImg icon = new AssetResponseImg();
					icon.setHeight(finalCreative.getIconH());
					icon.setWidth(finalCreative.getIconW());
					icon.setUrl(finalCreative.getIconUrl());
					assetIcon.setImg(icon);
					assets.add(assetIcon);
				}
				bidNative.setAssets(assets.toArray(new AssetResponse[assets.size()]));
				LinkDto link = new LinkDto();
				link.setUrl(finalCreative.getTargetUrl());
				bidNative.setLink(link);
				ad.setBidNative(bidNative);
			}
			ad.setAdmt(finalCreative.getAdmt().ordinal());
			ad.setAdUrl(finalCreative.getUrl());
			ad.setClickUrl(finalCreative.getTargetUrl());
			ad.setAdWidth(Float.valueOf(finalCreative.getWidth()).intValue());
			ad.setAdHeight(Float.valueOf(finalCreative.getHeight()).intValue());
			ad.setAdvertiserId(finalCreative.getCreateUserId());
			ad.setAdid(String.valueOf(finalCreative.getActivityId()));
			ad.setAdct(1);
			ad.setImageUrl(finalCreative.getImageUrl());
			ads.add(ad);
		}
		SeatBid seatBid = new SeatBid();

		if (ads.size() > 0) {
			Bid[] bids = new Bid[ads.size()];
			ads.toArray(bids);
			seatBid.setBids(bids);
			response.setBidding(true);
			Common.WriteLog("app bidding finish and success!bid size is " + ads.size() + ", requestid is "
					+ request.getRequestId());
			// TODO
			for (Bid ad : ads) {
				NodeData nd = nodeDataMap.get(ad.getCid());
				nd.setExitNum(1);
			}
		} else {
			Common.WriteLog("app bidding finish and failed! requestid is " + request.getRequestId());
		}
		response.setSeatbids(new SeatBid[] { seatBid });
		return response;
	}
}
